/**
 * Created by gastrodia on 14-8-16.
 */
var path = require('path');
var request = require('supertest');

var utils = module.exports = {};
var isNumberVal = function(val){
    return /^\d*$/.test(val);
}

var isAddress = function(val){
    return /^http.*/.test(val)
}

var getConfig = function(){
    return require(path.join(utils.getProjectRoot(),'backend/src/config'));
}



var getSimpleSitePort = function(){
    return getConfig().simpleSiteConfig.PORT;
}

utils.getCore = function(){
    return require(path.join(utils.getProjectRoot(),'backend/src/core'));
}



utils.getSimpleSiteTestRequest = function () {
    return request('http://localhost:' + getSimpleSitePort());
}

utils.cloneJson = function(ob){
    return JSON.parse(JSON.stringify(ob));
};

exports.getReqCookie = function(req){
    return req.cookies.split(';').shift();
}

utils.getResCookie = function(res){
    //当请求中没有cookie, server会在res中给客户端指定set-cookie
    return res.req._headers.cookie || res.headers['set-cookie'][0].split(';').shift();
}

utils.getSessionId = function(cookieValue){
    return cookieValue.split('.')[1].split('%')[1];
}

utils.getProjectRoot = function(){
    return path.join(__dirname,'../../../../');
}

utils.getTestRoot = function(){
    return path.join(__dirname,'../../');
}

if(require.main === module){
    utils.getProjectRoot();
}